<template>
    <div class="detail">
        <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav" />
        <scroll class="content" ref="scroll" @scroll="contentScroll" :probe-type="3">
            <detail-swiper :top-images="topImages" />
            <detail-base-info :goods="goods" />
            <detail-shop-info :shop="shop" />
            <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"/>
            <detail-param-info :paramInfo="paramInfo" ref="param"/>
            <detail-comment-info :comment-info="commentInfo" ref="comment" />
            <goods-list :goods="recommends" ref="recommend" />
        </scroll>
        <detail-bottom-bar @addCart="addToCart" />
        <back-top @click.native="backClick" v-show="isShowBackTop" />
        <!-- <toast :message="message" :show="show" /> -->
    </div>
</template>


<script>
import DetailNavBar from './childComps/DetailNavBar' //顶部导航
import DetailSwiper from './childComps/DetailSwiper' //轮播图
import DetailBaseInfo from './childComps/DetailBaseInfo' //商品信息
import DetailShopInfo from './childComps/DetailShopInfo' //店铺详情信息
import DetailGoodsInfo from './childComps/DetailGoodsInfo' //商品详情信息
import DetailParamInfo from './childComps/DetailParamInfo' //商品参数信息
import DetailCommentInfo from './childComps/DetailCommentInfo' //评论信息
import DetailBottomBar from './childComps/DetailBottomBar' //底部工具栏
// import BackTop from 'components/content/backTop/BackTop'//返回顶部按钮

import Scroll from 'components/common/scroll/Scroll' //滚动
import GoodsList from 'components/content/goods/GoodsList' //推荐商品
// import Toast from 'components/common/toast/Toast' //购买弹窗

import {getDetail,Goods,Shop,GoodsParam,getRecommend} from 'network/detail'//请求数据
import {debounce} from 'common/utils'//防抖动
import {itemListenerMixin,backTopMixin} from 'common/mixin' //封装的方法2  混入 导入

import {mapActions} from 'vuex'

export default {
    name:'Detail',
    components:{
        DetailNavBar,
        DetailSwiper,
        DetailBaseInfo,
        DetailShopInfo,
        DetailGoodsInfo,
        DetailParamInfo,
        DetailCommentInfo,
        DetailBottomBar,
        // BackTop,

        Scroll,
        GoodsList,
        // Toast
    },
    mixins:[itemListenerMixin,backTopMixin],//封装的方法2   混入
    data(){
        return {
            iid:null,//详情页数据
            topImages:[],//顶部图片数据
            goods:{},//商品信息
            shop:{},//店铺信息
            detailInfo:{},//保存商品的详情信息
            paramInfo:{},//商品参数
            commentInfo:{},//取出评论信息
            recommends:[],//推荐数据
            themeTopYs:[],//主题的高度值
            getThemeTopY:null,

            // message:"",
            // show:false,

            currentIndex:0,//记录position对应index值
            // isShowBackTop:false,//回到顶部按钮

            // itemImgListener:null, //方法2
        }
    },
    created(){
        //1.保存传入的iid
        this.iid = this.$route.params.iid

        //2.根据iid请求详情页数据
        getDetail(this.iid).then(res => {
            //1.获取顶部的图片轮播数据
            // console.log(res)
            const data = res.result
            this.topImages = data.itemInfo.topImages

            //2.获取商品信息
            this.goods = new Goods(data.itemInfo,data.columns,data.shopInfo.services)

            //3.创建店铺信息的对象
            this.shop = new Shop(data.shopInfo)

            //4.保存商品的详情数据
            this.detailInfo = data.detailInfo

            //5.获取参数的信息
            this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)

            //6.取出评论的信息
            if(data.rate.cRate !== 0){
                this.commentInfo = data.rate.list[0]
            }

            //1.第一次获取，值不对
            //值不对的原因：this.$refs.param.$el没有渲染
            // this.themeTopYs = []

            // this.themeTopYs.push(0);
            // this.themeTopYs.push(this.$refs.param.$el.offsetTop);
            // this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
            // this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);

            // console.log(this.themeTopYs)


            // this.$nextTick(() => {
            //     //2.第二次获取：值不对
            //     //值不对的原因:图片没有计算在内
            //     //根据最新的数据，对应的DOM是已经被渲染出来
            //     //但是图片依然是没有加载完(目前获取到offsetTop不包含其中的图片)
            //     //offsetTop值不对的时候，都是因为图片的问题
            //     this.themeTopYs = []

            //     this.themeTopYs.push(0);
            //     this.themeTopYs.push(this.$refs.param.$el.offsetTop);
            //     this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
            //     this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);

            //     console.log(this.themeTopYs)
            // })

        })

        //3.请求推荐数据
        getRecommend().then(res => {
            // console.log(res)
            this.recommends = res.data.list
        })

        //4.给getThemeTopY赋值(对给this.themeTopYs赋值的操作进行防抖)
        this.getThemeTopY = debounce(() => {
            // console.log('11111')
            this.themeTopYs = []
            this.themeTopYs.push(0);
            this.themeTopYs.push(this.$refs.param.$el.offsetTop - 44);
            this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44);
            this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 44);
            // 方法二
            this.themeTopYs.push(Number.MAX_VALUE)

            // console.log(this.themeTopYs)
        },100)
    },
    activated(){
        this.$refs.scroll.refresh()
    },
    mounted(){
        //方法1：
        // this.$bus.$on('detailItemImageLoad',() => {
        //     refresh()
        // })

        //方法2
        // const refresh = debounce(this.$refs.scroll.refresh,200)

        // this.itemImgListener = () => {
        //     refresh()
        // }

        // this.$bus.$on('itemImgLoad',this.itemImgListener)
    },
    updated(){
            
    },
    destroyed(){
    //   console.log('destroyed')
        this.$bus.$off('itemImgLoad',this.itemImgListener)
    },
    methods:{
        ...mapActions(['addCart']),
        imageLoad(){
            this.refresh()

            this.getThemeTopY()
        },
        // 点击nav导航跳转到相应的详情位置
        titleClick(index){
            // console.log(index)
            this.$refs.scroll.scrollTo(0,-this.themeTopYs[index],300)
        },
        // 滚动到详情对应的位置，nav导航名字颜色跟随改变
        contentScroll(position){
            // console.log(position)
            //1.获取y值
            const positionY = -position.y

            //2.positionY和主题中值进行对比
            //[0, 15010, 15938, 16312 ]
            //positionY 在 0     和 15010 之间，index = 0
            //positionY 在 =15010 和 15938 之间，index = 1
            //positionY 在 15938 和 16312 之间，index = 2
            //positionY 在大于等于 16312 以上，index = 3
            let length = this.themeTopYs.length

            // 方法一
            // for(let i = 0;i < length;i++){
            //     if(this.currentIndex !== i && ((i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1]) || (i == length - 1 && positionY >= this.themeTopYs[i]))){
            //         this.currentIndex = i
            //         this.$refs.nav.currentIndex = this.currentIndex
            //     }
            // }

            //2.positionY和主题中值进行对比
            //方法二
            for(let i = 0;i < length - 1;i++){
                    if(this.currentIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])){
                        this.currentIndex = i
                        this.$refs.nav.currentIndex = this.currentIndex
                }
            }

            //3.是否显示回到顶部
            this.isShowBackTop = (-position.y) > 1000
        },
        // backClick(){
        // // console.log('回到顶部')
        // this.$refs.scroll.scrollTo(0,0,500)
        // },
        //点击购物车按钮
        addToCart(){
             //1.获取购物车需要展示的信息
             const product = {}
             product.image = this.topImages[0]
             product.title = this.goods.title
             product.desc = this.goods.desc
             product.price = this.goods.realPrice
             product.iid = this.goods.iid 

             //2.将商品添加到购物车里
            this.addCart(product).then(res => {
                //方法1
                // console.log(res)
                // this.show = true
                // this.message = res

                // setTimeout(() => {
                //     this.show = false
                //     this.message = ""
                // },1500)

                //方法2
                this.$toast.show(res,1500)
                this.$toast.bbb()
            })

            //  this.$store.commit('addCart',product)

            // this.$store.dispatch('addCart',product).then(res => {
            //     console.log(res);
            // })
        }
    }
}
</script>



<style scoped>
    .detail {
        position: relative;
        z-index: 9;
        background-color: #fff;
        height: 100vh;
    }

    .detail-nav {
        position: relative;
        z-index: 9;
        background-color: #fff;
    }

    .content {
        height: calc(100% - 44px - 49px);
        overflow: hidden;
    }
</style>